// AWTTextComponent.cpp: implementation of the AWTTextComponent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "awt.h"
#include "AWTTextComponent.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTTextComponent, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void AWTTextComponent::removeNotify() 
{
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
	  peer->getText(m_text);
		m_selectionStart = peer->getSelectionStart();
		m_selectionEnd = peer->getSelectionEnd();
	}
	AWTComponent::removeNotify();
}

void AWTTextComponent::setText(CString& t) 
{
	m_text = t;
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
	  peer->setText(m_text);
	}
}

CString& AWTTextComponent::getText() 
{
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
	  peer->getText(m_text);
	}
	return m_text;
}

void AWTTextComponent::setEditable(BOOL b) 
{
	if (m_editable == b) 
	{
		return;
  }

	m_editable = b;
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
		peer->setEditable(b);
	}
}

int AWTTextComponent::getSelectionStart() 
{
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
//	  peer->GetSel(m_selectionStart,e);
		m_selectionStart = peer->getSelectionStart();
	}
	return m_selectionStart;
}

int AWTTextComponent::getSelectionEnd() 
{
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
//	  peer->GetSel(s,m_selectionEnd);
	  m_selectionEnd = peer->getSelectionEnd();
	}
	return m_selectionEnd;
}

void AWTTextComponent::select(int selectionStart, int selectionEnd)
{
	CString text = getText();
	if (selectionStart < 0) 
	{
	  selectionStart = 0;
	}
	if (selectionStart > text.GetLength()) 
	{
	  selectionStart = text.GetLength();
	}
	if (selectionEnd > text.GetLength()) 
	{
	  selectionEnd = text.GetLength();
	}
	if (selectionEnd < selectionStart) 
	{
	  selectionEnd = selectionStart;
	}

	m_selectionStart = m_selectionStart;
	m_selectionEnd = m_selectionEnd;

  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
	  peer->select(selectionStart, selectionEnd);
	}
}

void AWTTextComponent::selectAll() 
{
	CString text = getText();
	m_selectionStart = 0;
	m_selectionEnd = getText().GetLength();

  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
	  peer->select(m_selectionStart, m_selectionEnd);
	}
}

void AWTTextComponent::setCaretPosition(int position) 
{
	if (position < 0) 
	{
	  return;
	}

	int maxposition = getText().GetLength();
	if (position > maxposition) 
	{
	  position = maxposition;
	}

  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	if (peer != NULL) 
	{
//		CPoint pt = peer->PosFromChar(position);
//		peer->SetCaretPos(pt);
		peer->setCaretPosition(position);
	} 
}

int AWTTextComponent::getCaretPosition() 
{
  AWTTextComponentPeer* peer = dynamic_cast<AWTTextComponentPeer*>(getPeer());
	int position = 0;
	if (peer != NULL) 
	{
//		CPoint pt = peer->GetCaretPos();
//	  position = peer->CharFromPos(pt);
		position = peer->getCaretPosition();
	} 
	return position;
}

void AWTTextComponent::processTextEvent(AWTTextEvent* e) 
{
	if (m_textListener != NULL) 
	{
		int id = e->getId();
	  switch (id) 
		{
			case AWTTextEvent::TEXT_VALUE_CHANGED:
				m_textListener->textValueChanged(e);
				break;
	  }
  }
}
